package com.situ.mall.controller.back;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Result;
import com.situ.mall.util.Cookieutil;
import com.situ.mall.util.SimpleDateUtil;

@Controller
// @RequestMapping("/index")
public class IndexController {

	@RequestMapping("/index.action")
	public String getIndexPage() {
		// return "/WEB-INF/back/index.jsp";
		return "index";
	}
	
	@RequestMapping("/indexV1.action")
	public String getIndexV1Page() {
		// return "/WEB-INF/back/index_v1.jsp";
		return "index_v1";
	}
	
	@RequestMapping("/NewUser.action")
	@ResponseBody
    public Result NewUser(HttpServletRequest request, HttpServletResponse response,Model model) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		String lastTime=null;
		String last = null;
        Cookie [] cookies = request.getCookies();
        for(int i=0 ;cookies!=null&&i<cookies.length;i++){
            if("time1".equals(cookies[i].getName())){
                lastTime=cookies[i].getValue();
                break;
            }
        }
        if(lastTime==null){
        	last = "您今天第一次访问该网站";
        }else {
        	last = "您上次访问时间为："+lastTime+"";
        }
        String time = new SimpleDateFormat("YYYY-MM-dd-hh:mm:ss").format(new Date());
        Cookie cookie=new Cookie("time1",time);
        response.addCookie(cookie);
        return Result.success(last);
    }
	
	
}
